package org.hanbo.code.repository.action;

import java.util.ArrayList;
import java.util.List;

import org.hanbo.code.repository.exception.ErrorDescription;
import org.hanbo.code.repository.persistence.entity.CodeSnippet;
import org.hanbo.code.repository.persistence.entity.CodeSnippetDAO;

public class IndexAction extends GaeActionSupportBase 
{
	private static final long serialVersionUID = -7787458852603478876L;
	
	private List<CodeSnippetListItem> currentUserCodeSnippets;
	private List<CodeSnippetListItem> allPublicViewableCodeSnippets;
	private ErrorDescription errorDesc;
	
	public String execute()
	{
		if (!super.checkLogin())
		{
			//System.out.println("Not logged in");
			return super.getCurrentStageValue();
		}
		
		if (!super.checkRegistration())
		{
			//System.out.println("Not registered");
			return super.getCurrentStageValue();
		}
		
		String userEmail = super.getLoggedInUser().getEmail();
		
		CodeSnippetDAO codeSnippetFinder = new CodeSnippetDAO();
		try
		{
			codeSnippetFinder.acquirePersistenceManager();
			List<CodeSnippet> codeSnippetsByCurrentUser
				= codeSnippetFinder.findAllSnippetsByAuthorEmail(userEmail, 0, 20);
			List<CodeSnippet> codeSnippetsPublicViewable
				= codeSnippetFinder.findAllPublicViewableSnippets(0, 20);
			
			if (codeSnippetsByCurrentUser != null)
			{
				this.currentUserCodeSnippets
					= CodeSnippetListItem.createListOfCodeSnippetsForTable(codeSnippetsByCurrentUser);
			}
			
			if (codeSnippetsPublicViewable != null)
			{
				this.allPublicViewableCodeSnippets
					= CodeSnippetListItem.createListOfCodeSnippetsForTable(codeSnippetsPublicViewable);
			}
		}
		catch(Exception e)
		{
			errorDesc = new ErrorDescription(1000, e);
			return ERROR;
		}
		finally
		{
			codeSnippetFinder.releasePersistenceManager();
		}

		return SUCCESS;
	}
		
    public String getRedirectLogin()
    {
    	return GaeActionSupportBase.createLogInLink("../index.action");
    }

    public String getRedirectLogout()
    {
    	return GaeActionSupportBase.createLogOutLink("../index.action");
    }

	public String getRegisteredUserDisplayName()
	{
		return super.getRegisteredUser().getUserDisplayName();
	}
	
	public boolean getRegisteredUserStatus()
	{
		return super.checkUserStatus();
	}
	
	public boolean getCurrentuserCodeSnippetEmpty()
	{
		return (this.currentUserCodeSnippets == null
			|| this.currentUserCodeSnippets.size() == 0);
	}
	
	public List<CodeSnippetListItem> getCurrentUserCodeSnippets()
	{
		if (this.currentUserCodeSnippets == null)
		{
			this.currentUserCodeSnippets = new ArrayList<CodeSnippetListItem>();
		}
		return this.currentUserCodeSnippets;
	}

	public boolean getAllPublicViewableCodeSnippetsEmpty()
	{
		return (this.allPublicViewableCodeSnippets == null
			|| this.allPublicViewableCodeSnippets.size() == 0);
	}

	public List<CodeSnippetListItem> getAllPublicViewableCodeSnippets()
	{
		if (this.allPublicViewableCodeSnippets == null)
		{
			this.allPublicViewableCodeSnippets = new ArrayList<CodeSnippetListItem>();
		}
		return this.allPublicViewableCodeSnippets;
	}
	
	public ErrorDescription getErrorDescription()
	{
		return errorDesc;
	}
}
